package jsi.search;

import java.io.IOException;

import jsi.ws.Dict;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.index.Payload;

import jsi.ws.DictClient;

public class PolishWordsFilter extends TokenFilter {

	Log log = LogFactory.getLog(this.getClass());
	
	public PolishWordsFilter(TokenStream input) {
		super(input);
	}

	 public final Token next(Token result) throws IOException {
		    result = input.next(result);
		    if (result != null) {

		      final char[] buffer = result.termBuffer();
		    
		      final int length = result.termLength();
		      final String derivate = new String(buffer).substring(0, length);
		      
		      DictClient dict = DictClient.getInstance();
		      String basic = dict.basic(derivate);
		      if (basic!= null) {
		    	  result.setTermText(basic);
		      }
		      log.debug("dbg1 " + length + " " + derivate + " " + basic + " new size " + result.termLength());
		      
		      //for(int i=0;i<length;i++)
		      //  buffer[i] = Character.toLowerCase(buffer[i]);

		      return result;
		    } else
		      return null;
		  }

	 public Token next() throws IOException {
		    Token result = super.next();

		    if (result != null) {
		    	final char[] buffer = result.termBuffer();
			    
			      final int length = result.termLength();
			      final String derivate = new String(buffer).substring(0, length);
			      
			      DictClient dict = DictClient.getInstance();
			      String basic = dict.basic(derivate);
			      if (basic!= null) {
			    	  result.setTermText(basic);
			      }
			      log.debug("dbg2 " + length + " " + derivate + " " + basic + " new size " + result.termLength());
		    }      
		    return result;
		  }
	 
}
